//
//  ReplaceViewController.m
//  1243
//
//  Created by 杨森 on 16/5/11.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "ReplaceViewController.h"
#import "NetWorkTools.h"
#import "LoginViewController.h"

@interface ReplaceViewController ()<UIAlertViewDelegate>



@property(nonatomic,strong)UITextField *OldPassword;

@property(nonatomic,strong)UITextField *NewPassword;

@property(nonatomic,strong)UITextField *NewFirstPassword;

@end

@implementation ReplaceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.titleName;
    
    [self addSubViews];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)addSubViews
{
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
    
    if ([self.titleName isEqualToString:@"重置登录密码"]) {
        label.text = @"设置登录密码";
    }else{
        label.text = @"设置支付密码";
    }
    
    label.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:label];
    
    UITextField *OldPassword = [[UITextField alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(label.frame)+20, WEITH-20, 40)];
    OldPassword.placeholder = @"请输入您的原始密码";
    OldPassword.borderStyle  = UITextBorderStyleRoundedRect;
    OldPassword.secureTextEntry = YES;
    [self.view addSubview:OldPassword];
    self.OldPassword = OldPassword;
    
    
    UITextField *NewFirstPassword = [[UITextField alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(OldPassword.frame)+20, WEITH-20, 40)];
    NewFirstPassword.placeholder = @"请输入您的新密码";
    NewFirstPassword.borderStyle  = UITextBorderStyleRoundedRect;
    NewFirstPassword.secureTextEntry = YES;
    [self.view addSubview:NewFirstPassword];
    self.NewFirstPassword = NewFirstPassword;
    
    
    UITextField *newPassword = [[UITextField alloc] initWithFrame:CGRectMake(10,CGRectGetMaxY(NewFirstPassword.frame)+20, WEITH-20, 40)];
    newPassword.placeholder = @"请您再次输入新密码";
    newPassword.borderStyle  = UITextBorderStyleRoundedRect;
    newPassword.secureTextEntry = YES;
    self.NewPassword = newPassword;
    [self.view addSubview:newPassword];
    
    UIButton *finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [finishBtn setTintColor:[UIColor whiteColor]];
    [finishBtn setTitle:@"完成" forState:UIControlStateNormal];
    [finishBtn setBackgroundColor:NAVBAR_COLOR];
//    [finishBtn setBackgroundColor:[UIColor orangeColor]];
    finishBtn.frame = CGRectMake(10, CGRectGetMaxY(newPassword.frame)+20 , WEITH-20, 40);
    finishBtn.layer.cornerRadius = 5;
    finishBtn.layer.masksToBounds = YES;
    [finishBtn addTarget:self action:@selector(finishBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:finishBtn];
    
}

- (void)finishBtnClick
{
    NSLog(@"finishBtnClick");
    
    if ([self.NewFirstPassword.text isEqualToString:self.NewPassword.text]) {
        [self getReplaceData];
    }else {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您两次输入的新密码不一致" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alert.tag = 1236;
        [alert show];
    }

}

-(void)getReplaceData
{
    NSDictionary *dict = [ReadLoginData readLoginData];

    
    if (dict.count) {
        
        NSString *userId = [dict objectForKey:@"Number"];
        
        if ([self.titleName isEqualToString:@"重置登录密码"]) {
            NSDictionary *params = @{
                                     //
                                     @"Type":@"1",
                                     @"OldPassword":self.OldPassword.text,
                                     @"NewPassword":self.NewPassword.text,
                                     @"UserId":userId,
                                     @"Sign":@"07D4040C2A3C842997B2177D68BFC7756F553E4E"
                                     };
            
            [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"UPDATEPASSWORDINFO" completeBlock:^(id object) {
                NSLog(@"%@",object);
                
                if ([object[@"Message"] isEqualToString:@"修改成功"]) {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:object[@"Message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    alert.tag = 1234;
                    [alert show];
                }else{
                    
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:object[@"Message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    alert.tag = 1200;
                    [alert show];
                    
                }
                
                
                
                NSLog(@"1");
            }];
            
        }else{
            NSDictionary *params = @{
                                     //
                                     @"Type":@"2",
                                     @"OldPassword":self.OldPassword.text,
                                     @"NewPassword":self.NewPassword.text,
                                     @"UserId":userId,
                                     @"Sign":@"07D4040C2A3C842997B2177D68BFC7756F553E4E"
                                     };
            
            [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"UPDATEPASSWORDINFO" completeBlock:^(id object) {
                NSLog(@"%@",object);

                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:object[@"Message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                alert.tag = 1235;
                [alert show];
            }];
            
        }
    }
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1234 || alertView.tag == 1235 || alertView.tag == 1200) {
        self.NewPassword.text = @"";
        self.NewFirstPassword.text = @"";
        self.OldPassword.text = @"";
    }
    
    if (alertView.tag == 1234) {
        [self pushLogin];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    
    if (alertView.tag == 1236) {
        self.NewPassword.text = @"";
        self.NewFirstPassword.text = @"";
    }
    
}

- (void)pushLogin
{
    LoginViewController *a = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:[NSBundle mainBundle]];
    [self presentViewController:a animated:YES completion:nil];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.OldPassword resignFirstResponder];
    [self.NewPassword resignFirstResponder];
    [self.NewFirstPassword resignFirstResponder];
}



@end
